﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using System.Drawing;

public partial class QuanLySanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int index = 0;
            try
            {
                index = int.Parse(Request.QueryString["index"]);
            }
            catch (Exception) { }
            LoadDuLieu(index);
        }
    }

    private void LoadDuLieu(int count)
    {
        List<SanPhamDTO> listSP = SanPhamBUS.LayDanhSachSanPhamBoQuaNSanPham(count*10);

        CollectionPager3.PageSize = 10;
        CollectionPager3.DataSource = listSP;
        CollectionPager3.BindToControl = dtlSanPham;
        dtlSanPham.DataSource = CollectionPager3.DataSourcePaged;
        if (count != 0)
        {
            imbPrevious.Enabled = true;
            imbPrevious.BorderColor = Color.Orange;
            imbNext.Enabled = true;
            imbNext.BorderColor = Color.Orange;
            if (count*10 + 10 >= SanPhamBUS.DemSoLuongSanPham())
            {
                imbNext.Enabled = false;
                imbNext.BorderColor = Color.DarkGray;
            }
        }
        else
        {
            imbPrevious.Enabled = false;
            imbPrevious.BorderColor = Color.DarkGray;
            imbNext.Enabled = true;
            imbNext.BorderColor = Color.Orange;
        }
    }

    protected void dtlSanPham_ItemCommand(object source, DataListCommandEventArgs e)
    {
        Label ID = (Label)dtlSanPham.Items[e.Item.ItemIndex].FindControl("lbID");
        TextBox TenSanPham = (TextBox)dtlSanPham.Items[e.Item.ItemIndex].FindControl("txtTenSanPham");
        TextBox GiaBanHienTai = (TextBox)dtlSanPham.Items[e.Item.ItemIndex].FindControl("txtGiaBanHienTai");
        TextBox SoLuotXem = (TextBox)dtlSanPham.Items[e.Item.ItemIndex].FindControl("txtSoLuotXem");
        TextBox SoLuotMua = (TextBox)dtlSanPham.Items[e.Item.ItemIndex].FindControl("txtSoLuotMua");
        ImageButton HinhSanPham = (ImageButton)dtlSanPham.Items[e.Item.ItemIndex].FindControl("imbID");
        ImageButton imgEdit = (ImageButton)dtlSanPham.Items[e.Item.ItemIndex].FindControl("imbEdit");
        FileUpload fi = (FileUpload)dtlSanPham.Items[e.Item.ItemIndex].FindControl("fiulImage");
        Button upload = (Button)dtlSanPham.Items[e.Item.ItemIndex].FindControl("bttUpload");
        if (e.CommandName == "ChiTietSanPham")
        {
            if (SanPhamBUS.KiemTraSanPhamLaLapTop(int.Parse(ID.Text)))
            {
                Response.Redirect("~/ChiTietLapTop.aspx?ID=" + ID.Text);
            }
            else
            {
                Response.Redirect("~/ChiTietDienThoai.aspx?ID=" + ID.Text);
            }
        }

        if (e.CommandName == "EditImage")
        {
            
            
            if (fi.Visible == false)
            {
                fi.Visible = true;
                upload.Visible = true;
            }
            else
            {
                fi.Visible = false;
                upload.Visible = false;
            }
        }

        if (e.CommandName == "Upload")
        {
            
            fi.Visible = false;
            upload.Visible = false;

            SanPhamDTO sanpham = SanPhamBUS.LaySanPhamTheoID(int.Parse(ID.Text));
            sanpham.HinhSanPham = UploadImage(fi, sanpham.HinhSanPham);
            
       
            SanPhamBUS.CapNhatSanPham(sanpham);
        }

        if (e.CommandName == "Edit")
        {
            if (TenSanPham.ReadOnly == true)
            {
                HienThiDeChinhSua(TenSanPham, GiaBanHienTai, SoLuotXem,
                    SoLuotMua, imgEdit, false, "~/Images/update.png", Color.White);
            }
            else
            {
                HienThiDeChinhSua(TenSanPham, GiaBanHienTai, SoLuotXem,
                    SoLuotMua, imgEdit, true, "~/Images/edit.png", Color.DarkGray);
                SanPhamDTO sanpham = SanPhamBUS.LaySanPhamTheoID(int.Parse(ID.Text));
                sanpham.TenSanPham = TenSanPham.Text;
                sanpham.GiaBanHienTai = long.Parse(GiaBanHienTai.Text);
                sanpham.SoLuotXem = int.Parse(SoLuotXem.Text);
                sanpham.SoLuotMua = int.Parse(SoLuotMua.Text);
                SanPhamBUS.CapNhatSanPham(sanpham);
            }
        }

        if (e.CommandName == "Delete")
        {
            SanPhamBUS.XoaSanPhamTheoID(int.Parse(ID.Text));
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }

    private static void HienThiDeChinhSua(TextBox TenSanPham, TextBox GiaBanHienTai, TextBox SoLuotXem, 
        TextBox SoLuotMua, ImageButton imgEdit, bool b, string path, Color color)
    {
        TenSanPham.ReadOnly = b;
        GiaBanHienTai.ReadOnly = b;
        SoLuotXem.ReadOnly = b;
        SoLuotMua.ReadOnly = b;

        TenSanPham.BackColor = color;
        GiaBanHienTai.BackColor = color;
        SoLuotXem.BackColor = color;
        SoLuotMua.BackColor = color;

        imgEdit.ImageUrl = path;

    }


    private string UploadImage(FileUpload fi, string sdefault)
    {
        if (fi.HasFile == true)
        {
            string path = Server.MapPath("~/Images/" + fi.FileName);
            fi.SaveAs(path);
            return fi.FileName;
        }
        return sdefault;
    }

    protected void imbNext_Click(object sender, ImageClickEventArgs e)
    {
        int index = 0;
        try
        {
            index = int.Parse(Request.QueryString["index"]);
        }
        catch (Exception) { }
        index++;
        Response.Redirect("QuanLySanPham.aspx?index=" + index.ToString());
    }

    protected void imbPrevious_Click(object sender, ImageClickEventArgs e)
    {
        int index = 0;
        try
        {
            index = int.Parse(Request.QueryString["index"]);
        }
        catch (Exception) { }
        index--;
        Response.Redirect("QuanLySanPham.aspx?index=" + index.ToString());
    }

 
}

